﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract;
using System.Diagnostics;
using KerriganDataContract.Authentification;
using KerriganDataContract.Person;
using KerriganDataContract.Customer;

namespace KerriganService.DAC
{
    public static class AuthenticationDAC
    {

        public static AuthenticateUserDC CheckLoginUser(string nickName, string password)
        {
            TPerson entity = new TPerson();
            AuthenticateUserDC bo = new AuthenticateUserDC();
            //Boolean isValid = false;
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    //entity = db.TPersons.FirstOrDefault(f => f.NickName == nickName && f.Password == password);
                    //entity = db.TPersons.Where(f => f.NickName == nickName && f.Password == password).FirstOrDefault();
                    entity = (from TPerson p in db.TPersons where p.NickName == nickName && p.Password == password select p).FirstOrDefault();
                    //entity = db.TPersons.Select(f => f.NickName == nickName && f.Password == password);
                    //entity = from f in entity where f. select f;
                    bo = AuthenticationDAC.BoToCopyEntity(entity);
                    
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }

        public static CustomerDC CheckLoginCustomer(string nickName, string password)
        {
            TCustomer entity = new TCustomer();
            CustomerDC bo = new CustomerDC();
            
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = (from TCustomer p in db.TCustomers where p.TPerson.NickName == nickName && p.TPerson.Password == password select p).FirstOrDefault();
                    bo = CustomerDAC.CopyEntityToBo(entity);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }


        private static AuthenticateUserDC BoToCopyEntity(TPerson entity)
        {
            AuthenticateUserDC bo = new AuthenticateUserDC();

            try
            {
                bo.Person = new PersonDC();
                bo.Person.IdPerson = entity.IdPerson;
                bo.Person.NickName = entity.NickName;
                bo.Person.Password = entity.Password;
                bo.Person.FirstName = entity.FirstName;
                bo.Person.LastName = entity.LastName;
                bo.Person.City = entity.City;
                bo.Person.Adress = entity.Adress;
                bo.Person.Email = entity.Email;
                bo.Person.PhoneNumber = entity.PhoneNumber;
                bo.Person.PostCode = entity.PostCode;
                bo.Person.Type = entity.IdPersonType;
                
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }


        public static AuthenticateUserForumDC CheckLoginUserForum(string nickName, string password)
        {
            TPerson entity = new TPerson();
            AuthenticateUserForumDC bo = new AuthenticateUserForumDC();
            using (KerriganDBEntities db = new KerriganDBEntities())
            {
                try
                {
                    entity = (from TPerson p in db.TPersons where p.NickName == nickName && p.Password == password select p).FirstOrDefault();
                    bo = AuthenticationDAC.BoToCopyEntityForum(entity);

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return bo;
        }



        private static AuthenticateUserForumDC BoToCopyEntityForum(TPerson entity)
        {
            AuthenticateUserForumDC bo = new AuthenticateUserForumDC();

            try
            {
                bo.IdPerson = entity.IdPerson;
                bo.NickName = entity.NickName;
                bo.Password = entity.Password;
                bo.FirstName = entity.FirstName;
                bo.LastName = entity.LastName;
                bo.PersonType = new PersonTypeDC();
                bo.PersonType.IdPersonType = entity.IdPersonType;
                bo.PersonType.NameType = entity.TPersoneType.Name;

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

    }
}